<?php

App::uses('StringUtil', 'Lib');


class GenericUtil {


	public static function getLocations($location) {
        $Provincia=ClassRegistry::init('Provincia');
        $Comune=ClassRegistry::init('Comune');
		if (empty($location)) {
			return null;
		}
		$ret = array($location);
        if(intval($location)<1000){
            //ho trovato una regione..
            $province = $Provincia->find("list",array("recursive" => -1,"fields"=>array("Provincia.id"),"conditions"=>array("Provincia.regione_id"=>$location)));
			foreach ($province as $key => $provincia) {
				array_push($ret,$provincia);
				$comuni = $Comune->find("list", array("recursive" => -1,"fields"=>array("Comune.id"),"conditions"=>array("Comune.provincia_id"=>$provincia)));
				foreach ($comuni as $key2 => $comune) {
					array_push($ret,$comune);
				}
			}
        } else if(intval($location)>=1000 && intval($location)<10000){
            //ho trovato una provincia..
			$comuni = $Comune->find("list", array("recursive" => -1,"fields"=>array("Comune.id"),"conditions"=>array("Comune.provincia_id"=>$location)));
			foreach ($comuni as $key2 => $comune) {
				array_push($ret,$comune);
			}
			$Provincia->recursive=-1;
			$Provincia->read(null,$location);
			array_push($ret,$Provincia->field("regione_id"));
        } else if (intval($location)>=10000) {
	        //ho trovato una comune..
	        $Comune->recursive = 1;
			$comune = $Comune->read(null, $location);
			array_push($ret,$comune["Provincia"]["id"]);
			array_push($ret,$comune["Provincia"]["regione_id"]);
        }
		// print_r($ret);die;
		return $ret;
	}

	 
	public static function recipientsToArray($recipients) {
		if (empty($recipients) ) {
			return null;
		}
		$recipientsArray = explode(",",str_replace(";", ",", preg_replace('/\s+/', '',$recipients)));
		return $recipientsArray;
	}
    
    public static function generate_key($len = 8) { 
     // $salt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
     $salt = Configure::read('Security.salt');
     $cipherSeed = Configure::read('Security.cipherSeed');
	 return substr(str_shuffle($salt.$cipherSeed),0,$len);
    }
	
	public static function reArrayFiles(&$file_post) {
	    $file_ary = array();
	    $file_count = count($file_post['name']);
	    $file_keys = array_keys($file_post);
	
	    for ($i=0; $i<$file_count; $i++) {
	        foreach ($file_keys as $key) {
	            $file_ary[$i][$key] = $file_post[$key][$i];
	        }
	    }
	
	    return $file_ary;
	}

}

?>